//
//  AddressViewController.m
//  Mahjong
//
//  Created by Chen on 16/9/18.
//  Copyright © 2016年 Elliot. All rights reserved.
//

#import "AddressViewController.h"
#import "AddressTableViewCell.h"
#import "AddAddressViewController.h"
#import "AppTools.h"
#import "addressModel.h"
static NSString *identifierForAddressTableViewCell = @"AddressTableViewCell";


@interface AddressViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dateArray;

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *tel;
@property (nonatomic, copy) NSString *address;

@end

@implementation AddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"地址管理";
    
    [self createTableView];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_but_back"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction)];
    backItem.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItem = backItem;
    [self create];
}
-(void)create{
    self.dateArray = [NSMutableArray array];
    [AppTools createAFN:[NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=GetAddress&userid=%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userid"]] success:^(id result) {
//        NSLog(@"%@",[result[0] objectForKey:@"date"]);
        for (NSDictionary *dic in [result[0] objectForKey:@"date"]) {
            addressModel *model = [[addressModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dateArray addObject:model];
            
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
        
    }];
}
- (void)backAction{
    
    [[NSUserDefaults standardUserDefaults] setObject:self.address forKey:@"MRAddress"];
    [[NSUserDefaults standardUserDefaults] setObject:self.name forKey:@"MRName"];
    [[NSUserDefaults standardUserDefaults] setObject:self.tel forKey:@"MRTel"];
    
    [self.navigationController popViewControllerAnimated:YES];
    
    if (self.string != nil) {
        self.string(self.num,self.address,self.name,self.tel);
        
    }
}
-(void)blockstring:(stringBlock)str{
    self.string = str;
}

- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight) style:(UITableViewStyleGrouped)];
    
    self.tableView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerNib:[UINib nibWithNibName:identifierForAddressTableViewCell bundle:nil] forCellReuseIdentifier:identifierForAddressTableViewCell];
    
    
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return self.dateArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0){
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"service_but_write_pop"];
    cell.textLabel.text = @"添加收货地址";
    return cell;
    } else {
        AddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForAddressTableViewCell forIndexPath:indexPath];
        [cell string:^(NSInteger str, NSString *address, NSString *name, NSString *tel) {
            
            self.num = str;
            self.address = address;
            self.name = name;
            self.tel= tel;
        }];
//        [cell string:^(NSInteger str) {
//            
//            self.num = str;
//        }];
        
        addressModel *model = self.dateArray[indexPath.row];
        cell.name.text = model.name;
        cell.tel.text = model.tel;
        cell.address.text = [NSString stringWithFormat:@"%@%@%@",model.city,model.street,model.address];
        cell.num = model.Id;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 60;
    }
    return 120;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        AddAddressViewController *addAddressVC = [[AddAddressViewController alloc] init];
        [self.navigationController pushViewController:addAddressVC animated:YES];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    //设置separatorInset(iOS7之后)
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    //设置layoutMargins(iOS8之后)
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    //设置separatorInset(iOS7之后)
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    //设置layoutMargins(iOS8之后)
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
